	package client.gui;

import java.awt.Color;
import sharedspace.*;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.*;
import sharedspace.User;
import client.gui.GUI_AbstractMainMenu.MainMenuType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import messages.MessageAllExams;
import messages.MessageAllExecuteExams;
import messages.MessageApprovedTestByExecuteTest;
import messages.MessageApprovedTestByExecuteTestReply;
import messages.MessageCourse;
import messages.MessageStudentListByExam;
import messages.MessageStudentListByExamReply;
import messages.MessageSubject;
import messages.MessageSubjectReply;
import messages.MessageTeacherSubject;

import java.io.IOException;
import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.List;
/**
 * This JPanel purpose is to show the all grades of students in the database
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *  *
 */
public class GUI_Manager_ViewGrades extends GUI_AbstractMainMenu{
	private JScrollPane gradesScroller = null;
	private DefaultListModel gradesList = null;
	private JList listGrades = null;
	private JTable gradeJTable = null;
	private List<ApprovingGrade> grList = null;
	public JButton btnBack = null;
	private ArrayList<Course> courses;
	private ArrayList<ExecuteTest> ext;
	public JComboBox<String> comboBoxSubject;
	public JComboBox<String> comboBoxCourse;
	public JComboBox<String> comboBoxYear;
	public JComboBox<String> comboBoxSemester;
	private MessageSubjectReply msgsrep;
	private String executeTest;
	
	Client_GUI ms=null;
	


	public GUI_Manager_ViewGrades(Client_GUI MS) {
		super(MainMenuType.MANAGER,MS);
		this.ms = MS;
		this.initiateManager();
		greetMsg.setText("Choose Course:");
		mainMenu.setText("Manager");

	}
	
	/**
	 * Building the panel contents 
	 */	
	public void initiateManager()
	{
		this.setSize(1000, 700);	
		this.setLayout(null);
		background.add(getbtnBack(),null);
		//background.add(getExecuteExamsTable(),null);
		background.add(getGradesTable(),null);
		background.add(btnBack);
		
		comboBoxSubject= new JComboBox<String>();
		comboBoxSubject.setBounds(53, 147, 150, 30);
		background.add(comboBoxSubject);

		
		comboBoxCourse= new JComboBox<String>();
		comboBoxCourse.setSelectedIndex(-1);
		comboBoxCourse.setEnabled(false);
		comboBoxCourse.setBounds(210, 147, 150, 30);
		background.add(comboBoxCourse);
		
		comboBoxYear= new JComboBox<String>();
		comboBoxYear.setSelectedIndex(-1);
		comboBoxYear.setEnabled(false);
		comboBoxYear.setBounds(370, 147, 150, 30);
		background.add(comboBoxYear);
		
		comboBoxSemester= new JComboBox<String>();
		comboBoxSemester.setSelectedIndex(-1);
		comboBoxSemester.setEnabled(false);
		comboBoxSemester.setBounds(530, 147, 150, 30);
		background.add(comboBoxSemester);
		
		JLabel lblNewLabel = new JLabel("Grades:");
		lblNewLabel.setFont(new Font("Arial", Font.BOLD, 24));
		lblNewLabel.setForeground(Color.BLUE);
		lblNewLabel.setBounds(53, 201, 208, 40);
		background.add(lblNewLabel);
		
		getSubject();
		
	}

	private JButton getbtnBack()
	{
		if (btnBack == null)
		{
			btnBack = new JButton("Back");	
			btnBack.setBounds(53, 28, 107, 30);
			btnBack.setVisible(true);
		}
		
		return btnBack;
	}

	
	private void getSubject(){
	int i;
	MessageSubject mas = new MessageSubject();
	try{	
		ms.client.sendToServer(mas);// get list of subject
		msgsrep = (MessageSubjectReply) ms.client.GetMessage();
		comboBoxSubject.removeAllItems();
		comboBoxSubject.addItem("Choose Subject");
		for(i=0;i<msgsrep.getSubjectList().size();i++)
			comboBoxSubject.addItem(msgsrep.getSubjectList().get(i).getSubjectName());
	} 
	catch (IOException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
	} 
		
	comboBoxSubject.addActionListener(new ActionListener() {
		int i;
			public void actionPerformed(ActionEvent e1) {
				if(comboBoxSubject.getSelectedIndex()>0){   //if a subject was selected 
					gradesList.clear();
					comboBoxYear.removeAllItems();
					comboBoxCourse.removeAllItems();
					comboBoxSemester.removeAllItems();
					comboBoxYear.setSelectedIndex(-1);
					comboBoxCourse.setSelectedIndex(-1);
					comboBoxSemester.setSelectedIndex(-1);
					comboBoxCourse.setEnabled(true);
					comboBoxYear.setEnabled(false);
					comboBoxSemester.setEnabled(false);
					for (i = 0 ; i<comboBoxSemester.getActionListeners().length; i++)
					{ 	comboBoxSemester.removeActionListener(comboBoxSemester.getActionListeners()[i]);
						comboBoxYear.removeActionListener(comboBoxYear.getActionListeners()[i]);
						comboBoxCourse.removeActionListener(comboBoxCourse.getActionListeners()[i]);
					}
					getCourseBySubject(msgsrep.getSubjectList().get(comboBoxSubject.getSelectedIndex()-1));
				}//end if
				else
				{
					comboBoxCourse.setEnabled(false);  // course not selected , don't display the teacher list
					comboBoxYear.setEnabled(false);
					comboBoxSemester.setEnabled(false);
					gradesList.clear();
				}
			}

		});	
	} //end initializeIt

			
	private void getCourseBySubject(Subject sub){
		int i;
		MessageCourse mc = new MessageCourse(sub.getSubjectID());
		try{
			ms.client.sendToServer(mc);// get list of courses
			courses = (ArrayList<Course>)ms.client.GetMessage();
			comboBoxCourse.removeAllItems();
			gradesList.clear();
			for(i=0;i<courses.size();i++)
				comboBoxCourse.addItem(courses.get(i).getCourseName()); 
			comboBoxCourse.setSelectedIndex(-1);
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} 
		
		comboBoxCourse.addActionListener(new ActionListener() {
			int i;
			public void actionPerformed(ActionEvent e2) {
				if(comboBoxCourse.getSelectedIndex()>-1){   //if a course was selected 
					comboBoxYear.setEnabled(true);
					comboBoxSemester.setEnabled(false);
					comboBoxSemester.removeAllItems();
					comboBoxYear.removeAllItems();
					comboBoxSemester.removeAllItems();
					comboBoxYear.setSelectedIndex(-1);
					comboBoxSemester.setSelectedIndex(-1);	
					for (i = 0 ; i<comboBoxSemester.getActionListeners().length; i++)
					{ 	comboBoxSemester.removeActionListener(comboBoxSemester.getActionListeners()[i]);
						comboBoxYear.removeActionListener(comboBoxYear.getActionListeners()[i]);
						//comboBoxCourse.removeActionListener(comboBoxCourse.getActionListeners()[i]);
					}
					getYear(courses.get(comboBoxCourse.getSelectedIndex()));
					gradesList.clear();
				}//end if
				else
				{
					comboBoxYear.setEnabled(false);  // course not selected , don't display the teacher list
					comboBoxSemester.setEnabled(false);
					gradesList.clear();
				}
			}

		});	
		
		
	}
	
	
	private void getYear(Course course){
		int i;
		MessageAllExecuteExams mc = new MessageAllExecuteExams();
		try{
			ms.client.sendToServer(mc);// get list of Years
			ext = (ArrayList<ExecuteTest>)ms.client.GetMessage();
			gradesList.clear();
			for(i=0;i<ext.size();i++)
			{
				if((ext.get(i).getTest().getCourse().getCourseName()).equals(comboBoxCourse.getSelectedItem()))
					comboBoxYear.addItem(""+ext.get(i).getTestYear());
			}
			comboBoxYear.setSelectedIndex(-1);
			} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} 
		
	comboBoxYear.addActionListener(new ActionListener() {
		int i;
		public void actionPerformed(ActionEvent e3) {
			if(comboBoxYear.getSelectedIndex()>-1){   //if a subject was selected 
				comboBoxSemester.removeAllItems();
				comboBoxSemester.setSelectedIndex(-1);
				comboBoxSemester.setEnabled(true);
				for (i = 0 ; i<comboBoxSemester.getActionListeners().length; i++)
				{ 	comboBoxSemester.removeActionListener(comboBoxSemester.getActionListeners()[i]);
					//comboBoxYear.removeActionListener(comboBoxYear.getActionListeners()[i]);
					//comboBoxCourse.removeActionListener(comboBoxCourse.getActionListeners()[i]);
				}
				getSemester(courses.get(comboBoxCourse.getSelectedIndex()));
				gradesList.clear();
			}//end if
			else
			{
				comboBoxSemester.setEnabled(false);  // course not selected , don't display the teacher list
				gradesList.clear();
			}
		}

	});

		
	}// end getYear
	
	
	private void getSemester(Course course){
		int i;
		MessageAllExecuteExams mc = new MessageAllExecuteExams();
		try{
			ms.client.sendToServer(mc);// get list of Semesters
			ext = (ArrayList<ExecuteTest>)ms.client.GetMessage();
			comboBoxSemester.removeAllItems();
			for(i=0;i<ext.size();i++)
			{
				if((""+ext.get(i).getTestYear()).equals(comboBoxYear.getSelectedItem()) && ((ext.get(i).getTest().getCourse().getCourseName()).equals(comboBoxCourse.getSelectedItem())))
					{ 
					 	comboBoxSemester.addItem(""+ext.get(i).getTestSemster());
					}
			}
		 	comboBoxSemester.setSelectedIndex(-1);

			
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} 
		
	comboBoxSemester.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e4) {
			int i;
			if(comboBoxSemester.getSelectedIndex()>-1){   //if a semester was selected 
				for(i=0;i<ext.size();i++)	
				{
				if(((""+ext.get(i).getTestYear()).equals(comboBoxYear.getSelectedItem()) && ((ext.get(i).getTest().getCourse().getCourseName()).equals(comboBoxCourse.getSelectedItem()))) && ((""+ext.get(i).getTestSemster()).equals(""+comboBoxSemester.getSelectedItem())))
					{	executeTest = ext.get(i).getExecuteTestID();
						System.out.println(executeTest.toString());	
					}
				}
					MessageApprovedTestByExecuteTest msgATE=new MessageApprovedTestByExecuteTest(executeTest);
						try {
								//Sending the login message to server
								
								ms.client.sendToServer(msgATE);	
				
								} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} 

						//Getting reply to login from client
						MessageApprovedTestByExecuteTestReply msgATErep = (MessageApprovedTestByExecuteTestReply) ms.client.GetMessage();	
						grList = msgATErep.getAlist();
						gradesList.clear();
						for(i=0;i<msgATErep.getAlist().size();i++)
						{
							gradesList.addElement("Student ID: " + msgATErep.getAlist().get(i).getStudentInTest().getStudent().getUserID() + "   ,Name: " + msgATErep.getAlist().get(i).getStudentInTest().getStudent().getUserFirstName() +  msgATErep.getAlist().get(i).getStudentInTest().getStudent().getUserLastName() + "   ,Exam Grade: " + msgATErep.getAlist().get(i).getStudentFianlGrade());
						}
					
				
			}//end if
		}
	});
	
	}// end getSemester
	
	private JScrollPane getGradesTable()
	{
		int i;
		if (gradeJTable == null)
		{
			gradesList = new DefaultListModel();
			listGrades = new JList(gradesList);
			listGrades.setVisibleRowCount(3);
			listGrades.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listGrades.setSelectedIndex(0);
			gradesScroller = new JScrollPane(listGrades);
			gradesScroller.setBounds(53, 250, 774, 225);
		} 
		return gradesScroller;
	}
}